package com.mr.groupcomparator;

import java.io.IOException;

import org.apache.hadoop.io.DataInputBuffer;
import org.apache.hadoop.io.RawComparator;

/*
 * 1.继承WritableComparator
 * 2.实现RawComparator
 */

public class MyGroupingCompartor implements RawComparator<GroupComparator>{

	private GroupComparator key1 = new GroupComparator();
	private GroupComparator key2 = new GroupComparator();

	private DataInputBuffer buffer = new DataInputBuffer();

	@Override
	public int compare(GroupComparator o1, GroupComparator o2) {

		return o1.getOrderId().compareTo(o2.getOrderId());
	}

	@Override
	public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {

		try {
			buffer.reset(b1, s1, l1);                   // parse key1
			key1.readFields(buffer);

			buffer.reset(b2, s2, l2);                   // parse key2
			key2.readFields(buffer);

			buffer.reset(null, 0, 0);                   // clean up reference
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		return compare(key1, key2);
	}

}
